import heapq
class Solution(object):
    def mostBooked(self, n, meetings):
        meetings.sort()
        m = len(meetings)
        st1 = [i for i in range(n)]
        st2 = []
        d = {i: 0 for i in range(n)}
        index = 0
        time = 0
        while index < m:
            while st2 and st2[0][0] == time:
                _, id = heapq.heappop(st2)
                heapq.heappush(st1, id)
            while len(st2) < n and index < m and meetings[index][0] <= time:
                node = heapq.heappop(st1)
                heapq.heappush(st2, [meetings[index][1] - meetings[index][0] + time, node])
                d[node] += 1
                index += 1
            if not st2:
                if index < m:
                    time = meetings[index][0]
                else:
                    break
            else:
                if len(st2) == n:
                    time = st2[0][0]
                else:
                    if index < m:
                        time = min(st2[0][0], meetings[index][0])
                    else:
                        break
        max_count = max(d.values())
        for key in d:
            if d[key] == max_count:
                return key


data = Solution()
n = 2
meetings = [[0,10],[1,5],[2,7],[3,4]]
print(data.mostBooked(n, meetings))